package io.renren.modules.sys.controller;

import java.util.*;

import io.renren.common.validator.ValidatorUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.ClasstimeEntity;
import io.renren.modules.sys.service.ClasstimeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 课次表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-10-31 17:53:34
 */
@RestController
@RequestMapping("sys/classtime")
public class ClasstimeController {
    @Autowired
    private ClasstimeService classtimeService;
    private Map result = new HashMap();

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:classtime:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = classtimeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:classtime:info")
    public R info(@PathVariable("id") Integer id){
        ClasstimeEntity classtime = classtimeService.getById(id);

        return R.ok().put("classtime", classtime);
    }

    /**
     * 信息
     */
    @RequestMapping("/infoName/{id}")
    public R infoName(@PathVariable("id") String id){
        String name = "";
        String[] strarr = id.split(",");
        if(strarr.length>0){
            for(String idstr:strarr){
                if ("null".equals(idstr)||StringUtils.isBlank(idstr)){
                    continue;
                }
                int ids = Integer.parseInt(idstr);
                ClasstimeEntity classtime = classtimeService.getById(ids);
                name+=classtime.getName()+",";
            }
        }
//        for (int i=0;i<id.length();i++) {
////            classid=id.substring(i, i+1);
//            classid = id.split(",");
//            if (",".equals(classid)) {
//                continue;
//            }
//            classtime = classtimeService.getById(classid);
//            if (null!=classtime) {
//                name+=classtime.getName()+",";
//            }
//        }

        return R.ok().put("name", name);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:classtime:save")
    public R save(@RequestBody ClasstimeEntity classtime){
        classtimeService.save(classtime);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:classtime:update")
    public R update(@RequestBody ClasstimeEntity classtime){
        ValidatorUtils.validateEntity(classtime);
        classtimeService.updateById(classtime);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:classtime:delete")
    public R delete(@RequestBody Integer[] ids){
        classtimeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("getClasstimes")
    public R getCities() {
        result.clear();
        result.put("data", classtimeService.list());
        return R.ok(result);
    }
}
